//package pers.litao.bit.order.schedule;
//
//import cn.hutool.core.date.DateTime;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.json.JSONUtil;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.util.CollectionUtils;
//import pers.litao.bit.commons.entities.order.OrderEntity;
//import pers.litao.bit.commons.entities.order.RetailWeekEntity;
//import pers.litao.bit.commons.entities.order.SalesEntity;
//import pers.litao.bit.commons.entities.order.SupplierWeekEntity;
//import pers.litao.bit.commons.entities.priduct.GoodsEntity;
//import pers.litao.bit.commons.entities.user.AddressEntity;
//import pers.litao.bit.commons.entities.user.UserInfoEntity;
//import pers.litao.bit.commons.utils.R;
//import pers.litao.bit.order.openfeign.UserFeignService;
//import pers.litao.bit.order.service.OrderService;
//import pers.litao.bit.order.service.RetailWeekService;
//import pers.litao.bit.order.service.SalesService;
//import pers.litao.bit.order.service.SupplierWeekService;
//
//import java.math.BigDecimal;
//import java.text.NumberFormat;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// * @author 李涛
// * @date 2020-12-20 13:22
// */
//@Component
//@Slf4j
//public class OrderSchedule {
//    @Autowired
//    private OrderService orderService;
//
//    @Autowired
//    private RetailWeekService retailWeekService;
//
//    @Autowired
//    private SupplierWeekService supplierWeekService;
//
//    @Autowired
//    private SalesService salesService;
//
//
//    /**
//     * 零售商统计
//     * 每隔59秒执行一次
//     *
//     */
//    @Scheduled(cron = "*/59 * * * * ?")
//    @Transactional(rollbackFor = Exception.class)
//    public void realTimeUpdate(){
//        DateTime date = DateUtil.date();
//        DateTime dateTime = DateUtil.beginOfDay(date);
//        List<OrderEntity> orderList = orderService.list(new QueryWrapper<OrderEntity>()
//                .ge("create_time", dateTime.getTime()));
//        orderList.forEach(item -> {
//            if (item.getIsCalculate() == 0){
//                if(item.getOrderStatus() < 3){
//                    DateTime cdt = new DateTime(item.getCreateTime());
//                    DateTime bdt = DateUtil.beginOfDay(cdt);
//                    List<RetailWeekEntity> weekList = retailWeekService.list(new QueryWrapper<RetailWeekEntity>()
//                            .eq("time", bdt.getTime())
//                            .eq("user_id", item.getRetailersId()));
//                    if(CollectionUtils.isEmpty(weekList)){
//                        RetailWeekEntity retailWeekEntity = new RetailWeekEntity();
//                        retailWeekEntity
//                                .setBuyMoney(item.getMoney())
//                                .setUserId(item.getRetailersId())
//                                .setTime(bdt.getTime())
//                                .setStrTime(DateUtil.formatDate(bdt));
//                        retailWeekService.save(retailWeekEntity);
//                    }
//                    else {
//                        RetailWeekEntity retailWeekEntity = weekList.get(0);
//                        retailWeekEntity.setBuyMoney(retailWeekEntity.getBuyMoney().add(item.getMoney()));
//                        retailWeekService.updateById(retailWeekEntity);
//                    }
//                    item.setIsCalculate(1);
//                    orderService.updateById(item);
//                }
//            }
//            else {
//                if(item.getOrderStatus() == 3){
//                    DateTime cdt = new DateTime(item.getCreateTime());
//                    DateTime bdt = DateUtil.beginOfDay(cdt);
//                    List<RetailWeekEntity> weekList = retailWeekService.list(new QueryWrapper<RetailWeekEntity>()
//                            .eq("time", bdt.getTime())
//                            .eq("user_id", item.getRetailersId()));
//                    if(!CollectionUtils.isEmpty(weekList)){
//                        RetailWeekEntity retailWeekEntity = weekList.get(0);
//                        retailWeekEntity.setBuyMoney(retailWeekEntity.getBuyMoney().subtract(item.getMoney()));
//                        retailWeekService.updateById(retailWeekEntity);
//                    }
//                    item.setIsCalculate(0);
//                    orderService.updateById(item);
//                }
//            }
//        });
//        List<SalesEntity> salesList = salesService.list(new QueryWrapper<SalesEntity>()
//                .ge("create_time", dateTime.getTime()));
//        salesList.forEach(item -> {
//            if(item.getIsCalculate() == 0){
//                DateTime cdt = new DateTime(item.getCreateTime());
//                DateTime bdt = DateUtil.beginOfDay(cdt);
//                List<RetailWeekEntity> weekList = retailWeekService.list(new QueryWrapper<RetailWeekEntity>()
//                        .eq("time", bdt.getTime())
//                        .eq("user_id", item.getUserId()));
//                if(CollectionUtils.isEmpty(weekList)){
//                    RetailWeekEntity retailWeekEntity = new RetailWeekEntity();
//                    retailWeekEntity
//                            .setSellMoney(item.getMoney())
//                            .setUserId(item.getUserId())
//                            .setTime(bdt.getTime())
//                            .setStrTime(DateUtil.formatDate(bdt));
//                    retailWeekService.save(retailWeekEntity);
//                }
//                else {
//                    RetailWeekEntity retailWeekEntity = weekList.get(0);
//                    retailWeekEntity.setSellMoney(retailWeekEntity.getSellMoney().add(item.getMoney()));
//                    retailWeekService.updateById(retailWeekEntity);
//                }
//                item.setIsCalculate(1);
//                salesService.updateById(item);
//            }
//        });
//    }
//    @Scheduled(cron = "*/59 * * * * ?")  //一分钟执行一次
//    @Transactional(rollbackFor = Exception.class)
//    public void supplierWeek(){
//        for(int i = 6;i >=0 ;i--){
//            Long beginTime = DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.date(), i * -1)).getTime();
//            Long endTime = DateUtil.offsetDay(DateUtil.date(), i * -1).getTime();
//            List<OrderEntity> list = orderService.list(new QueryWrapper<OrderEntity>()
//                    .ge("create_time", beginTime)
//                    .lt("create_time", endTime));
//            Map<Long, List<OrderEntity>> collect = list.stream().collect(Collectors.groupingBy(OrderEntity::getSupplierId));
//            for(Map.Entry<Long,List<OrderEntity>> entry : collect.entrySet()){
//                long count = entry.getValue().stream()
//                        .filter(item -> {
//                            return item.getOrderStatus() < 3;
//                        }).count();
//                BigDecimal salesVolume = entry.getValue().stream()
//                        .filter(item -> {
//                            return item.getOrderStatus() < 3;
//                        })
//                        .map(OrderEntity::getMoney)
//                        .reduce(BigDecimal.ZERO, BigDecimal::add);
//
//                long chargebackCount = entry.getValue().size() - count;
//
//                String result = "0";
//                if(count != 0){
//                    NumberFormat numberFormat = NumberFormat.getInstance();
//                    numberFormat.setMaximumFractionDigits(4);
//                    result = numberFormat.format((double) chargebackCount/(double) count*100);
//                }
//                List<SupplierWeekEntity> supplierWeekEntities = supplierWeekService.list(new QueryWrapper<SupplierWeekEntity>()
//                        .eq("create_time", beginTime)
//                        .eq("user_id", entry.getKey()));
//                if(CollectionUtils.isEmpty(supplierWeekEntities)){
//                    SupplierWeekEntity supplierWeekEntity = new SupplierWeekEntity();
//                    supplierWeekEntity.setUserId(entry.getKey())
//                            .setCreateTime(beginTime)
//                            .setChargebackCount(chargebackCount)
//                            .setOrderCount(count)
//                            .setChargebackRate(BigDecimal.valueOf(Double.parseDouble(result)))
//                            .setSalesVolume(salesVolume);
//                    supplierWeekService.save(supplierWeekEntity);
//                }else {
//                    SupplierWeekEntity supplierWeekEntity = supplierWeekEntities.get(0);
//                    supplierWeekEntity.setUserId(entry.getKey())
//                            .setCreateTime(beginTime)
//                            .setChargebackCount(chargebackCount)
//                            .setOrderCount(count)
//                            .setChargebackRate(BigDecimal.valueOf(Double.parseDouble(result)))
//                            .setSalesVolume(salesVolume);
//                    supplierWeekService.updateById(supplierWeekEntity);
//                }
//            }
//        }
//
//    }
//}
